package ru.sgu.diploma.practic;

import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import ru.sgu.diploma.practic.domain.model.Automat;
import ru.sgu.diploma.practic.errors.AlphabetExcephion;
import ru.sgu.diploma.practic.errors.NoStartStateException;
import ru.sgu.diploma.practic.service.AutomatGenerator;
import ru.sgu.diploma.practic.service.impl.AutomatGeneratorImpl;

import java.io.*;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * User: datmakin
 * Date: 13.03.11
 * Email: atmakin.dv@gmail.com
 */

public class AutomatGeneratorTestCase {

    @Test
    public void test() {

    }

    public void testGenerate() throws FileNotFoundException, IOException, NoStartStateException, AlphabetExcephion {
        AutomatGenerator automatGenerator = new AutomatGeneratorImpl();

        File file = new File("testInput/automate_one.csv");
        FileReader reader = new FileReader(file);
        BufferedReader buffer = new BufferedReader(reader);

        String input = "";
        String temp = "";

        while((temp = buffer.readLine()) != null) {
            input += temp + "\n";
        }

        Automat automat = automatGenerator.generateAutomatFromFile(input);
        assertNotNull(automat);
        assertEquals(automat.getStateMap().size(), 2);

        String[] alphabet = new String[2];
        alphabet[0] = "a";
        alphabet[1] = "b";

        assertEquals(automat.getAlphabet(), alphabet);

        Set<String> stateNames = new HashSet<String>();
        stateNames.add("s1");
        stateNames.add("s2");

        List<String> nextStatesForS1 = new LinkedList<String>();

        nextStatesForS1.add("s1");
        nextStatesForS1.add("s2");

        assertEquals(automat.getStateMap().keySet().size(), 2);
        assertEquals(automat.getStateMap().keySet(), stateNames);
        assertEquals(automat.getStateMap().get("s1").getNextStates().get("a"), "s2");
        assertEquals(automat.getStateMap().get("s1").getNextStates().get("b"), "s1");

        assertEquals(automat.getStateMap().get("s1").getOutputs().get("b"), "b");
        assertEquals(automat.getStateMap().get("s1").getOutputs().get("a"), "b");

        assertEquals(automat.getStateMap().get("s2").getNextStates().get("b"), "s1");
        assertEquals(automat.getStateMap().get("s2").getNextStates().get("a"), "s2");

        assertEquals(automat.getStateMap().get("s2").getOutputs().get("b"), "a");
        assertEquals(automat.getStateMap().get("s2").getOutputs().get("a"), "b");

    }
}
